<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/29
 * Time: 17:44
 */
namespace app\common\model;

use think\Db;
use think\Model;
class TeamModel extends Model
{
    // 模型名
    protected $name = 'team';
    // 数据库表名
    protected $table = 'lv_team';

    /**
     * 获取后台所有用户信息
     * @param $where
     * @param string $page
     * @param string $limit
     * @param string $filed
     * @return array|false|\PDOStatement|string|\think\Collection|Model
     */
    public function getTeamList($where, $page = '', $limit = '', $filed = '')
    {
        if ($filed === '') {
            $filed = '*';
        }

        if ($page == '' && $limit == '') {
            $data = Db::name('team')
                ->field($filed)->where($where)
                ->find();
        } else {
            $data = Db::name('team')->alias('a')
                ->field('a.*,b.t_admin_id,b.t_admin_tel,b.pid,b.add_time')
                ->join('team_admin b','a.team_id=b.team_id',"LEFT")
                ->where($where)
                ->where('is_pass',1)
                ->where('b.pid',0)
                ->limit($page, $limit)
                ->order('a.team_id', 'desc')->select();
            //处理时间
            foreach($data as $k =>$v)
            {
                $data[$k]['examine_time'] = date('Y-m-d H:i:s',$v['examine_time']);

            }
        }

        return $data;
    }

    /**
     * 后台查询旅行社总数量
     * @param $where array 条件
     * @return int|string
     */
    public function getTeamCount($where)
    {
        $count = Db::name('team')->alias('a')
            ->field('a.*,b.t_admin_id,b.t_admin_tel,b.pid,b.add_time')
            ->join('team_admin b','a.team_id=b.team_id',"LEFT")
            ->where($where)
            ->where('is_pass',1)
            ->where('b.pid',0)
            ->count();
        return $count;
    }

    /*
     * 修改商户密码
     */
    public function editPass($where,$data)
    {
        return Db::name('team_admin')->where($where)->setField('password',$data);
    }

    /*
     * 删除
     */
    public function teamDel($where)
    {
        return Db::name('team')->where($where)->setField('is_del',1);
    }

    /*
     * 修改
     */
    public function edit($where,$data)
    {
        return DB::name('team')->where($where)->update($data);
    }

    public function getTeamInfoById($team_id,$field='')
    {
       if ($field === '') {
            $field = '*';
        }
        $where['is_pass'] = 1;
        $where['is_del'] = 0;
        $where['team_id'] = $team_id;
        $team = Db::name('team')->field($field)->where($where)->find();
        if(!empty($team)){
           $team['team_icon'] = addWaterByField($team['team_icon']);
            return $team;
        }
       return [];
    }
}